package org.ihelpuoo.orm.handler.dom;

import java.lang.reflect.Field;
import java.util.List;

import javax.xml.xpath.XPathConstants;

import org.ihelpuoo.orm.ObjectRule;
import org.ihelpuoo.orm.filter.Filter;
import org.ihelpuoo.orm.filter.SegmentObjectFilter;
import org.ihelpuoo.orm.handler.ObjectProcessor;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class ElementSequenceFieldUtils
{
	static List evalObjectList(ObjectProcessor processor, ObjectRule r, Field f, Object src,
			Filter filter, Object parent, Class subType) throws Exception
	{
		ObjectRule subRule = processor.getManual().getRule(subType);
		String tagName = r.getDomNodeName(f);
		Object child = processor.getManual().xpath().newXPath().evaluate("./" + tagName + "[1]",
				src,XPathConstants.NODE);
		if (null != child)
			return processor.query(subRule,child,subType,filter,null,null,null,null);
		return null;
	}

	static Filter evalObjectFilter(ObjectRule r, Field f, Object obj)
	{
		SegmentObjectFilter filter = r.getXPathFieldFilter(f);
		if (null != filter)
			filter.setObject(obj);
		return filter;
	}

	static Element prepareSubSource(ObjectRule r, Field f, Object src)
	{
		String tagName = r.getDomNodeName(f);
		Element ele = ((Node)src).getOwnerDocument().createElement(tagName);
		((Node)src).appendChild(ele);
		return ele;
	}

}
